<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Upgrade Settings
*/
class upgrade_settings
{
	var $refresh_after_step = true;
	var $await_input        = false;
	
	function upgrade_settings( &$installer )
	{
		$this->installer =& $installer;
		$this->ipsclass  =& $installer->ipsclass;
	}
	
	function run_step()
	{
		$this->installer->xml->xml_parse_document( file_get_contents( './data-install/ineo_settings.xml' ) );
		$settings = $this->installer->xml->xml_array;
		
		$settings = $settings['settingexport']['settinggroup']['setting'];

		foreach ( $settings as $key => $value )
		{
		  if ( $value['conf_is_title']['VALUE'] == 1 )
		  {
		    foreach ( $value as $value_key => $value_value )
		    {
		      $setting_group[$value_key] = $value_value['VALUE'];
		    }
		  }
		  else
		  {
		    foreach ( $value as $value_key => $value_value )
		    {
		      if ( $value_key != 'VALUE' && $value_key != 'conf_id' )
		      {
		        $setting_array[$key][$value_key] = $value_value['VALUE'];
		      }
		    }
		  }
		}

		// The setting group title.
		$title_array = array( 'conf_title_title' => $setting_group['conf_title_title'],
		                      'conf_title_desc' => $setting_group['conf_title_desc'],
		                      'conf_title_noshow' => $setting_group['conf_title_noshow'],
		                      'conf_title_keyword' => $setting_group['conf_title_keyword'] );

		// Insert the new group if it doesn't exist, 'cause it doesn't hurt to check!
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'conf_settings_titles', 'where' => 'conf_title_keyword = \'ineo\'' ) );
		$this->ipsclass->DB->simple_exec();

		if ( $this->ipsclass->DB->get_num_rows() == 0 )
		{
		  $this->ipsclass->DB->do_insert( 'conf_settings_titles', $title_array );
		  $title_id = $this->ipsclass->DB->get_insert_id();
		}

		foreach ( $setting_array as $key => $value )
		{
		  // Clean things up a bit.
		  unset( $value['conf_title_keyword'] );
		  unset( $value['conf_is_title'] );
		  $value['conf_value'] = '';
		  $value['conf_group'] = $title_id;
		  $this->ipsclass->DB->do_insert( 'conf_settings', $value );
		}

		require( ROOT_PATH . 'sources/action_admin/settings.php' );
		$setting_class = new ad_settings();
		$setting_class->ipsclass =& $this->ipsclass;
		$setting_class->setting_rebuildcache();
		
		// Set status text:
		$this->installer->status_text .= 'Settings added to the database.<br />';
	}
}

?>